package cn.chinasyq.photoquan.user.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.user.mode.UserMode;
import cn.chinasyq.photoquan.util.BitmapUtil;
import cn.master.util.utils.ToastUtil;
import cn.master.volley.models.pojo.Wrapper;
import cn.master.volley.models.response.handler.ResponseHandler;
import cn.master.volley.models.response.listener.ResponseForMultiPartListener;

public class AlbumUploadActivity extends FragmentActivity implements View.OnClickListener, ResponseHandler.OnSucceedListener, ResponseHandler.OnFailedListener, ResponseHandler.OnUploadingListener, ResponseHandler.OnNeedLoginListener {
    private static final String TAG_UPLOAD = "refresh";
    private TextView progress;
    private Button btn;
    private File bitmap;
    private ImageView iv;
    private View v;
    private ResponseForMultiPartListener<Wrapper<Object>> aa;

    public enum Status {
        uploading, cancle
    }

    Status status;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_album_upload);
        progress = (TextView) findViewById(R.id.progress);
        btn = (Button) findViewById(R.id.button);
        iv = (ImageView) findViewById(R.id.iv);
        bitmap = new File(BitmapUtil.convertUri2FilePath(this, getIntent().getData()));
        iv.setImageURI(getIntent().getData());
        v = findViewById(R.id.lv_progressbar);
        if (bitmap != null) {
            uploadiHeadPortrait(bitmap);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                switch (status) {
                    case uploading:
                        status = Status.cancle;
                        btn.setText("????");
                        aa.close();
                        finish();
                        break;
                    case cancle:
                        uploadiHeadPortrait(bitmap);

                        break;
                }
                break;
            default:
                break;
        }

    }

    private void uploadiHeadPortrait(File bitmap) {
        v.setVisibility(View.VISIBLE);
        btn.setText(R.string.cancle_upload);
        status = Status.uploading;
        ResponseHandler handler = new ResponseHandler(TAG_UPLOAD);
        handler.setOnSucceedListener(this);
        handler.setOnNeedLoginListener(this);
        handler.setOnFailedListener(this);
        handler.setOnUploadingListener(this);
        aa = UserMode.uploadAlbumPhoto(this, bitmap, handler);
    }

    @Override
    public void onFailed(String tag, ResponseHandler.FailedStatus status, int resultCode, String msg) {
        if (msg == null) {
            switch (status) {
                case DATA_ERROR:
                    ToastUtil.show(getApplicationContext(), R.string.tip_data_error);
                    break;
                case NETWORK_ERROR:
                    ToastUtil.show(getApplicationContext(), R.string.servererrortips);
                    break;
            }
        } else {
            ToastUtil.show(getApplicationContext(), msg);
        }
    }

    @Override
    public void onNeedLogin(String tag) {
        startActivity(new Intent(this, LoginActivity.class));
    }


    @Override
    public void onSucceed(String tag, boolean isCache, Object data) {
        if (tag.equals(TAG_UPLOAD)) {
            finish();
            ToastUtil.show(this,getString(R.string.upload_success));
        }
    }

    @Override
    public void onUploading(String tag, int progress) {
        this.progress.setText(progress + "%");
    }
}
